package com.etc.controller;

import com.etc.pojo.User;
import com.etc.pojo.Video;
import com.etc.service.VideoOfTypeService;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/VideoOfTypeController")
@Controller
public class VideoOfTypeController {

    Logger logger= Logger.getLogger(VideoOfTypeController.class);

    @Autowired
    VideoOfTypeService service;

    /**
     * 根据二级分类编号显示视频
     * @param request
     * @param response
     * @param pageNo
     * @param pageSize
     * @throws IOException
     */
    @RequestMapping("/showVideoById")
    private void showVideoById(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                               @RequestParam(value = "pageSize",defaultValue = "30") Integer pageSize) throws IOException {

        String targetListString = request.getParameter("targetList");
        String partListString = request.getParameter("partList");
        String degreeListString = request.getParameter("degreeList");
        String apparatusListString = request.getParameter("apparatusList");
        Gson gson = new Gson();

        List<Integer> targetList = gson.fromJson(targetListString, List.class);
        List<Integer> partList = gson.fromJson(partListString, List.class);
        List<Integer> degreeList = gson.fromJson(degreeListString, List.class);
        List<Integer> apparatustList = gson.fromJson(apparatusListString, List.class);


        if (targetList==null){
            targetList=new ArrayList<Integer>();
        }
        if (partList==null){
            partList=new ArrayList<Integer>();
        }
        if (degreeList==null){
            degreeList=new ArrayList<Integer>();
        }
        if (apparatustList==null){
            apparatustList=new ArrayList<Integer>();
        }
        logger.debug("这是degreeList的"+degreeList);

        PageInfo<Video> pageInfo = service.getVideoByType(targetList, partList, degreeList, apparatustList, pageNo, pageSize);
        for (Video video:pageInfo.getList()){
            logger.debug("这是返回的值:"+video);
        }
        String data = gson.toJson(pageInfo);
        response.getWriter().print(data);
    }

    @RequestMapping("/showVideoPlay")
    public ModelAndView showVideoPlay(Integer videoId, HttpSession session){
        User user = (User) session.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        Video video = service.getVideoById(videoId);
        /*判断是不是会员视频*/
        if (video.getPublishState()==1){

            /*如果是会员视频判断是否登录*/
            if (user!=null){
                /*如果已登录判断是不是会员*/
                if (user.getVipState()==1){
                    /*如果是会员跳转到视频播放页*/
                    mv.addObject("video",video);
                    mv.setViewName("forward:/playpage/playpage.jsp");
                }else {
                    /*如果不是会员跳转到会员注册页*/
                    mv.setViewName("forward:/front/PayMembers.jsp");
                }
            }else{
                /*如果未登录跳转到登录页*/
                mv.setViewName("forward:/login/userLogin.jsp");
            }


        }else {
            /*如果不是会员视频跳转到视频播放页*/
            mv.addObject("video",video);
            mv.setViewName("forward:/playpage/playpage.jsp");
        }

        return mv;
    }




}
